import { _decorator, Component, EventHandler, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Global, RelicData, RelicType } from '../../Util/Global';
import { Tools } from '../../Util/Tools';
import AssetsMgr from '../../Model/AssetsMgr';
import GameEventMgr, { GameEvent } from '../../Model/GameEventMgr';
import SoundCtrl from '../../Ctrl/SoundCtrl';
const { ccclass, property } = _decorator;

@ccclass('RelicItem')
export class RelicItem extends Component {

    @property({ type: Sprite })
    public icon: Sprite = null;
    @property({ type: Label })
    public txtTop: Label = null;
    @property({ type: Label })
    public txtButtom: Label = null;
    @property({ type: Sprite })
    public lockMask: Sprite = null;

    set relicType(value) {
        this._relicType = value;
        this.updateData();
    }
    get relicType() {
        return this._relicType;
    }
    _relicType: RelicType = RelicType.DysonTrackPlanA;

    relicData: RelicData = null;

    hasNum: number = 0;//拥有数量
    protected _activate: boolean = false;//激活
    get activate() {
        return this._activate;
    }
    set activate(val) {
        this.icon.color = val ? Tools.hex2color('#2AD8D8') : Tools.hex2color('#7E7E7E');
        this._activate = val;
    }

    protected start(): void {

        GameEventMgr.Register(this, GameEvent.OnPlayerRelicActivate, this.OnRelicActivateEventHandler.bind(this));
    }

    updateData() {
        this.hasNum = Global.gameManager.playerData.relics.get(this._relicType) || 0;
        // this.activate = num > 0;
        this.lockMask.node.active = this.hasNum <= 0;

        this.relicData = RelicData.RelicMap.get(this._relicType);
        this.txtButtom.string = this.hasNum > 0 ? '' : this.relicData.name;
        // this.icon.grayscale = this.hasNum <= 0;
        if (this.hasNum <= 0) {
            this.activate = false;
        }

        this.relicCheckActive();

        this.txtTop.string = this.hasNum > 0 ? this.hasNum + '' : '';
        AssetsMgr.getRes(this.relicData.iconPath, SpriteFrame, (icon) => {
            if (this.icon && this.icon.isValid) {
                this.icon.spriteFrame = icon;
            }
        });
    }

    public OnClick(event: EventHandler, customEventData: string) {
        let msg = this.relicData.des;

        if (this.hasNum <= 0) {
            GameEventMgr.Dispatch(GameEvent.TipGameMessage, "MOSS：" + msg, 5);
            SoundCtrl.playEffect('mouserelease1');
            return;
        }
        this.relicAction(!this.activate);
        SoundCtrl.playEffect('forceField_000', 0.2);

    }

    protected relicCheckActive() {
        let player = Global.gameManager.playerUnit;
        switch (this._relicType) {
            case RelicType.DysonTrackPlanA:
                this.activate = player.track_priority == 1;
                break;
            case RelicType.DysonTrackPlanB:
                this.activate = player.track_priority == -1;
                break;
            default: break;
        }

    }

    protected relicAction(active: boolean) {
        if (this.hasNum <= 0) {
            active = false;
        }
        this.activate = active;
        let player = Global.gameManager.playerUnit;
        switch (this._relicType) {
            case RelicType.DysonTrackPlanA:
                if (active) {
                    player.SetFixedTrackStrategy(true, 1);
                }
                else {
                    player.SetFixedTrackStrategy(false, 0);
                }
                break;
            case RelicType.DysonTrackPlanB:
                if (active) {
                    player.SetFixedTrackStrategy(true, -1);
                }
                else {
                    player.SetFixedTrackStrategy(false, 0);
                }
                break;
            default: break;
        }

        if (active) {
            GameEventMgr.Dispatch(GameEvent.OnPlayerRelicActivate, this._relicType);
        }
    }

    protected OnRelicActivateEventHandler(activateType: RelicType) {
        if (activateType == this._relicType) {
            return;
        }
        switch (activateType) {
            case RelicType.DysonTrackPlanA:
                if (this._relicType == RelicType.DysonTrackPlanB) {
                    this.activate = false;
                }
                break;
            case RelicType.DysonTrackPlanB:
                if (this._relicType == RelicType.DysonTrackPlanA) {
                    this.activate = false;
                }
                break;
            default: break;
        }

    }
}


